from django.conf import settings
from django.db.models import Avg, Sum, Count, Max, Min, Q, F
from django.forms.models import model_to_dict

from rest_framework import serializers
from rest_framework.serializers import *
from rest_framework.validators import UniqueValidator, UniqueTogetherValidator  # 验证器

# from allinpay import apidev as PAY
# from iClouds.apidev import py_str
from mysqldb.models import *
from mysqldb import redis as REDIS
from mysqldb.serializers import ListDjangoFilterBackend


class AllinpayPlaceviewsCreatSerializer(serializers.Serializer):
    """创建视图"""

    name = CharField(
        required=True,
        help_text="视图名称",
    )
    info = CharField(
        required=True,
        help_text="权限说明",
    )
    act = ChoiceField(
        choices=["GET", "POST", "PUT", "PATCH", "DELETE"],
        required=True,
        help_text="请求方法",
    )
    group = CharField(
        allow_blank=True,
        allow_null=True,
        required=True,
        help_text="分组",
    )

    class Meta:
        fields = "__all__"


class AllinpayHardwareListSerializer(serializers.ModelSerializer):
    """资产列表"""

    id = IntegerField(label="ID", read_only=True)
    localid = IntegerField(default=0, help_text="本地id")
    area = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="区域",
    )
    pcname = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="主机名",
    )
    ipaddr = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="IP地址",
    )
    mac = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="mac地址",
    )
    board = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="主板",
    )
    cpu = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="CPU",
    )
    memory = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="内存",
    )
    video = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="显卡",
    )
    display = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="显示器",
    )
    netadapter = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="网卡",
    )
    network = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="网络连接",
    )
    mouse = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="鼠标",
    )
    keyboard = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="键盘",
    )
    headset = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="音频设备",
    )
    cpu_temperature = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="cpu温度",
    )
    board_temperature = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="主板温度",
    )
    video_temperature = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="显卡温度",
    )
    speed = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="网卡速率",
    )
    status = IntegerField(default=1, help_text="状态")
    lasttime = DateTimeField(default=timezone.now, help_text="最近上传时间")
    pid = HiddenField(default=0, help_text="场所ID")

    class Meta:
        model = AllinpayHardwareList
        fields = "__all__"

    # def to_representation(self, instance):
    #     data = super().to_representation(instance)
    #     data = dict(data)
    #     print(data)
    #     return data


class AllinpayMemberListSerializer(serializers.ModelSerializer):
    """场所会员"""

    id = IntegerField(read_only=True)
    name = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="姓名",
    )
    idcode = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="身份证号码",
    )
    phone = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="联系电话",
    )
    openid = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="openid",
    )
    email = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="电子邮箱",
    )
    avatar = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=False,
        help_text="会员头像",
    )
    hash = CharField(allow_blank=True, allow_null=True, max_length=255, help_text="校验码")
    creattime = DateTimeField(allow_null=True, required=False)
    lasttime = DateTimeField(allow_null=True, required=False)
    pid = HiddenField(default=0, help_text="场所ID")
    localid = IntegerField(required=False, help_text="本地ID")

    class Meta:
        model = AllinpayMemberList
        fields = "__all__"
        validators = [
            UniqueTogetherValidator(
                queryset=AllinpayMemberList.objects.all(), fields=("pid", "hash")
            )
        ]


class AllinpayCallmeListSerializer(serializers.ModelSerializer):
    """呼叫列表"""

    class Meta:
        model = AllinpayCallmeList
        fields = "__all__"


class AllinpayStaffinterListSerializer(serializers.ModelSerializer):
    """员工积分"""

    class Meta:
        model = AllinpayStaffinterList
        fields = "__all__"


class AllinpayStaffinterLogSerializer(serializers.ModelSerializer):
    """员工积分日志"""

    class Meta:
        model = AllinpayStaffinterLog
        fields = "__all__"


class AllinpayMemberinterListSerializer(serializers.ModelSerializer):
    """会员积分"""

    class Meta:
        model = AllinpayMemberinterList
        fields = "__all__"


class AllinpayMemberinterLogsSerializer(serializers.ModelSerializer):
    """会员积分日志"""

    class Meta:
        model = AllinpayMemberinterLogs
        fields = "__all__"


class AllinpayMembermoneyListSerializer(serializers.ModelSerializer):
    """会员钱包"""

    class Meta:
        model = AllinpayMembermoneyList
        fields = "__all__"


class AllinpayMembermoneyLogsSerializer(serializers.ModelSerializer):
    """会员钱包日志"""

    class Meta:
        model = AllinpayMembermoneyLogs
        fields = "__all__"


class SyncWechatTemplateSerializer(serializers.Serializer):
    """微信公众号模板消息请求"""

    unids = ListField(child=CharField(), help_text="微信unionid列表")
    datas = ListField(child=CharField(), help_text="模板消息列表")
    template = ChoiceField(choices=["服务进度提醒", "服务评价提醒"], help_text="消息模板ID")

    class Meta:
        fields = "__all__"


class AllinpayPlaceVersionSerializer(serializers.ModelSerializer):
    """收集场所端版本"""

    id = IntegerField(label="ID", read_only=True)
    pid = IntegerField(help_text="场所ID")
    version = CharField(
        allow_blank=True,
        allow_null=True,
        help_text="版本号",
    )
    currenttime = DateTimeField(read_only=True)

    class Meta:
        model = AllinpayPlaceVersion
        fields = "__all__"

    def validate_pid(self, attr):
        if not attr in list(
            AllinpayPlaceList.objects.all().values_list("id", flat=True)
        ):
            raise serializers.ValidationError("场所ID错误")
        return attr


# print(repr(AllinpayPlaceVersionSerializer()))
